var now = getDay(0); 
var username = sessionStorage.getItem("name");
var userid = sessionStorage.getItem("userId");
var jobnumber = sessionStorage.getItem("jobnumber");
var repairid;
var calendar = new lCalendar();
var photoUrl = [];//拍照列表
var _config ;
var startdate;
var enddate;
var flag=1;
var starttimecalendar = new lCalendar();
var endtimecalendar = new lCalendar();
var shopcode;
starttimecalendar.init({
	'trigger': '#startDate',
	'type': 'date',
	'minDate': '2021-01-01'
});
endtimecalendar.init({
	'trigger': '#endDate',
	'type': 'date',
	'minDate': '2021-01-01'
});
function ICalendarClick(){

//	onload()
}
$(document).ready(function(){

	if(jobnumber ==0 || jobnumber == undefined || jobnumber == ""){
	     $('#shopcode')[0].style.display="none";
		 $('#shopcode1')[0].style.display="";
		 $('#search_img')[0].style.display="";
	}else{
		var jm = jobnumber.split(",");
		if(jm.length>0){
			for(var i=0;i<jm.length;i++){
				 $("#shopcode").append('<option value="'+jm[i]+'">'+jm[i]+'</option>');
			}
			setshopname($('#shopcode').val());
		}
		$("#shopcode").change(function(){
      		setshopname($('#shopcode').val());
   		});
	}
	$('#startDate').val(now);
	$('#endDate').val(now)
	$('#search_img').on('click',function(){
		if( $("#shopcode1").val() == ""  ||  $("#shopcode1").val() == undefined){
			alert("门店号不能为空")
		}else{
			setshopname($('#shopcode1').val());
		}
	});
	$('#searchButton').on('click',function(){
		if($('#startDate').val() > $('#endDate').val()){
			alert("开始日期不能大于结束日期")
		}else{
			startdate = $('#startDate').val();
			enddate = $('#endDate').val();
			getInfo();
		}
	});
	$("#username").prepend(username);
	search_time(2);
//	search_flag(1);
//	getInfo();
});
function getInfo(){
	document.getElementById("data").innerHTML = "";
	$.ajax({
		url: "../../repair_info/getSelectBody"
		,type:'post'
		,data:JSON.stringify({"startdate":startdate,"enddate":enddate,"flag":flag,"shopcode":shopcode})
		,contentType: "application/json; charset=utf-8"
		,dataType:'json'
		,success: function(res){
			var trStr;
			var flagname;
			for (var i = 0; i < res.data.length; i++) {
					if(res.data[i].flag==1){flagname="未接单"}
					else if(res.data[i].flag==21){flagname="未完成"}
					else if(res.data[i].flag==22){flagname="未完成"}
					else if(res.data[i].flag==3){flagname="待确认"}
					else if(res.data[i].flag==4){flagname="待评价"}
					else if(res.data[i].flag==5){flagname="已完成"}
					trStr += '<tr class="thin-border-bottom">'
							+'	<td>'+res.data[i].repairdate.substring(0,10)+'</span></td>'
							+'	<td class="orange" onclick = "detail(\''+res.data[i].repairid+'\',\''+res.data[i].flag+'\')">'+res.data[i].instruction+'</td>'
							+'	<td>'+formatCurrencyFixed1(Number(getHourChaBetween(res.data[i].repairdate,getNowFormatDateTime())))+'</td>'
							+'	<td>'+flagname+'</td>'
							+'</tr>'
			}
			$('#data').append(trStr);
		}
	});
}
function detail(repairid,flag){	
	var url;
	if(flag=='1'){
		url = "../../mobile_page/baoxiu/baoxiu_search?repairid="+repairid;//此处拼接内容
	}else if(flag=='21' || flag == '22'){
		url = "../../mobile_page/baoxiu/baoxiu_search_free?repairid="+repairid;//此处拼接内容
	}else if(flag == '4' || flag=='5'|| flag=='3'){
		url = "../../mobile_page/baoxiu/baoxiu_search_charge?repairid="+repairid+"&flag="+flag;//此处拼接内容
	}
	window.location.href = url;
}	
function search_time(time){
	$('#time1').text("");
	$(".child2")[0].style.backgroundColor="#599EF8";
	$(".child3")[0].style.backgroundColor="#599EF8";
	$(".child4")[0].style.backgroundColor="#599EF8";
	$(".child5")[0].style.backgroundColor="#599EF8";
	$(".child"+time)[0].style.backgroundColor="#BEBEBE";
	if(time == 5){
		$("#time1")[0].style.display="none";
		$("#time2")[0].style.display="";
	}else{
		$("#time1")[0].style.display="";
		$("#time2")[0].style.display="none";
	}
	if(time==2){
		$('#time1').append(now+' ~ '+now);startdate=now;enddate=now;
	}else if(time==3){
		$('#time1').append(getDay(-30)+' ~ '+now);startdate=getDay(-30);enddate=now;
	}else if(time==4){
		$('#time1').append(getDay(-90)+' ~ '+now);startdate=getDay(-90);enddate=now;
	}
	search_flag(flag);
}
function search_flag(f){
	flag=f;
	$(".child1")[1].style.backgroundColor="#599EF8";
	$(".child2")[1].style.backgroundColor="#599EF8";
	$(".child3")[1].style.backgroundColor="#599EF8";
	$(".child"+flag)[1].style.backgroundColor="#BEBEBE";
	getInfo();
}
function setshopname(code){
    shopcode =code;
	$('#shopname').html("");
	$.ajax({
			url: '../../repair_info/fillshopname'
			,type:'post'
			,dataType:'json'
			,contentType: "application/json; charset=utf-8"
			,data:JSON.stringify({'shopcode':shopcode})  
			,success: function(res){
				console.log(res);
				if(res.data.field != undefined){
					$('#shopname').prepend(res.data.field.deptname);
				}
				search_time(2);
			}
	});
}